package cn.zh.npro.Activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Adapter.CaAdapter;
import cn.zh.npro.Item.CaItem;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ConnectAc extends BaseActivity{
     String id;
     ListView clist;
     CaAdapter caAdapter=null;
     ArrayList<CaItem> caItems=new ArrayList<CaItem>();
     ImageView ccback;
     LinearLayout btom;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect);
        initca();
        }


    public void initca(){
        id=getIntent().getStringExtra("id");
        ccback=findViewById(R.id.ccback);
        ccback.setOnClickListener(this);

        clist=findViewById(R.id.clist);
        btom=findViewById(R.id.btom);
        caAdapter=new CaAdapter(ConnectAc.this,caItems,id);
        clist.setAdapter(caAdapter);
        btom.setOnClickListener(this);
        }

    @Override
    protected void onResume() {
        super.onResume();
         getCdent();
         }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.ccback:
                finish();
                break;
            case R.id.btom:
                Intent intent=new Intent(ConnectAc.this,EditClient.class);
                 intent.putExtra("type","1");
                 startActivity(intent);

                break;
           }
         }


    public void getCdent(){
        OkHttpUtils.get(BaseUrl.GetCustomerDetail)
                .params("customerId",id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                               JSONObject Res=js.getJSONObject("Res");

                               JSONArray ja = Res.getJSONArray("LinkmanList");
                                caItems.clear();
                                    for (int i=0;i<ja.length();i++){
                                        JSONObject ji=ja.getJSONObject(i);
                                        String linkmanid=ji.getString("linkmanid");
                                        String fullname=ji.getString("fullname");
                                        String mobile=ji.getString("mobile");
                                        String address=ji.getString("address");
                                        boolean isMaster=ji.getBoolean("isMaster");
                                        CaItem caItem=new CaItem(linkmanid,fullname,mobile,address,isMaster);
                                        caItems.add(caItem);
                                     }
                                 caAdapter.notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
           }







}
